package com.baimifan.renovation;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.baimifan.renovation.tools.PermissionsChecker;
import com.baimifan.renovation.tools.statusBar;

import java.util.Timer;
import java.util.TimerTask;

public class LoadingActivity extends AppCompatActivity implements View.OnClickListener {
    private Activity load;
    private PermissionsChecker checker;
    private String[] PERMISSIONS = new String[]{
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.READ_PHONE_STATE
    };
    private static final int REQUEST_CODE = 0;
    private ImageView loading_icon, loading_btn;
    private Float w, h, widthPixels, heightPixels;
    private CountDownTimer cuCountDownTimer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_loading);
        load = LoadingActivity.this;
        statusBar.setTranslucent(load);
        checker = new PermissionsChecker(load);
        getDisplay();
        init();
        setParams();
        if (Build.VERSION.SDK_INT >= 23) {
            checkroot();
        } else {

        }
    }

    private void init() {
        loading_btn = (ImageView) findViewById(R.id.loading_btn);
        loading_icon = (ImageView) findViewById(R.id.loading_icon);

        loading_btn.setOnClickListener(this);
        loading_icon.setOnClickListener(this);
    }

    private void getDisplay() {
        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        widthPixels = Float.valueOf(metrics.widthPixels);
        heightPixels = Float.valueOf(metrics.heightPixels);
        w = widthPixels / 360;
        h = heightPixels / 640;
    }

    private void setParams() {
        FrameLayout.LayoutParams params_icon = new FrameLayout.LayoutParams((int) (w * 109), (int) (h * 109));
        params_icon.setMargins((int) (w * 125.5), (int) (h * 150), 0, 0);
        loading_icon.setLayoutParams(params_icon);

        FrameLayout.LayoutParams params_btn = new FrameLayout.LayoutParams((int) (w * 121), (int) (h * 39.2));
        params_btn.setMargins((int) (w * 119.5), (int) (h * 580.4), 0, 0);
        loading_btn.setLayoutParams(params_btn);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE && resultCode == PermissionsActivity.PERMISSIONS_DENIED) {
        }
        sleep();
    }

    /**
     * 配置Android 6.0权限申请
     */
    private void checkroot() {
        if (checker.lacksPermissions(PERMISSIONS)) {
            PermissionsActivity.startActivityForResult(this, REQUEST_CODE, PERMISSIONS);
        } else {
            sleep();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.loading_icon:

                break;

            case R.id.loading_btn:
                cuCountDownTimer.cancel();
                load.finish();
                Intent intent = new Intent(load, LoginActivity.class);
                load.startActivity(intent);
                break;
        }
    }

    private void sleep() {
        cuCountDownTimer = new CountDownTimer(4000, 1000) {

            @Override
            public void onTick(long millisUntilFinished) {
                int count = (int) (millisUntilFinished / 1000);
                if ( count == 1) {
                    load.finish();
                    Intent intent = new Intent(load, LoginActivity.class);
                    load.startActivity(intent);
                }
            }

            @Override
            public void onFinish() {

            }
        }.start();
    }

}
